package com.kgc.warehouse.bussness.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.warehouse.bussness.entity.Goods;
import com.kgc.warehouse.bussness.entity.Inport;
import com.kgc.warehouse.bussness.entity.Provider;
import com.kgc.warehouse.bussness.service.GoodsService;
import com.kgc.warehouse.bussness.service.InportService;
import com.kgc.warehouse.bussness.service.ProviderService;
import com.kgc.warehouse.bussness.vo.InportVO;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import com.mysql.cj.util.StringUtils;
import com.sun.org.apache.bcel.internal.classfile.Code;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("inport")
public class InportController {
    @Resource
    private InportService inportService;

    @Resource
    private ProviderService providerService;

    @Resource
    private GoodsService goodsService;

    @RequestMapping("loadAllInport")
    public ResponseResult loadAllInport(InportVO inportVO){

        Page<Inport> inportPage = new Page<>(inportVO.getPage(),inportVO.getLimit());

        LambdaQueryWrapper<Inport> inportLambdaQueryWrapper = new LambdaQueryWrapper<>();
        inportLambdaQueryWrapper.eq(inportVO.getProviderid()!=null&&inportVO.getProviderid()!=0,Inport::getProviderid,inportVO.getProviderid());
        inportLambdaQueryWrapper.eq(inportVO.getGoodsid()!=null&&inportVO.getGoodsid()!=0,Inport::getGoodsid,inportVO.getGoodsid());
        inportLambdaQueryWrapper.ge(inportVO.getStartTime()!=null,Inport::getInporttime,inportVO.getStartTime());
        inportLambdaQueryWrapper.le(inportVO.getEndTime()!=null,Inport::getInporttime,inportVO.getEndTime());
        inportLambdaQueryWrapper.orderByDesc(Inport::getInporttime);
        Page<Inport> page = inportService.page(inportPage, inportLambdaQueryWrapper);

        List<Inport> inportList = page.getRecords();
        long total = page.getTotal();
        List<Inport> collect = inportList.stream().map(inport -> {
            Provider provider = providerService.getById(inport.getProviderid());
            if (ObjectUtils.isNotNull(provider)){
                inport.setProvidername(provider.getProvidername());
            }
            Goods goods = goodsService.getById(inport.getGoodsid());
            if (ObjectUtils.isNotNull(goods)) {
                inport.setGoodsname(goods.getGoodsname());
                inport.setSize(goods.getSize());
            }
            return inport;
        }).collect(Collectors.toList());

        return new ResponseResult(collect,total,CodeMsg.SUCCESS);
    }

    @RequestMapping("addInport")
    public ResponseResult addInport(InportVO inportVO,HttpSession session){
        try {
            if (inportVO.getNumber()<50) {
                return new ResponseResult(CodeMsg.NUMBER_UNLEGAL);
            }
            inportService.addInport(inportVO,session);
            return new ResponseResult(CodeMsg.ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.ADD_FAIL);
        }

    }

    @RequestMapping("deleteInport")
    public ResponseResult deleteInport(Integer id){
        try {
            inportService.deleteInport(id);
            return new ResponseResult(CodeMsg.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.DELETE_FAIL);
        }
    }

    @RequestMapping("updateInport")
    public ResponseResult updateInport(InportVO inportVO){
        try {
            if (inportVO.getNumber()<50) {
                return new ResponseResult(CodeMsg.NUMBER_UNLEGAL);
            }
            inportService.updateInport(inportVO);
            return new ResponseResult(CodeMsg.UPDATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.UPDATE_FAIL);
        }
    }

}
